package router

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"shop-order-pro/handler"
)

func Cors() gin.HandlerFunc {

	return func(c *gin.Context) {
		method := c.Request.Method
		origin := c.Request.Header.Get("Origin")
		if origin != "" {
			c.Header("Access-Control-Allow-Origin", "*")
			c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
			c.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization")
			c.Header("Access-Control-Allow-Credentials", "true")
			c.Set("content-type", "application/json")
		}
		if method == "OPTIONS" {
			c.AbortWithStatus(http.StatusNoContent)
		}
		c.Next()
	}
}
func Init() *gin.Engine {

	r := gin.Default()
	r.Use(Cors())
	r.Static("/static", "./static")
	r.GET("/", func(c *gin.Context) {
		c.String(http.StatusOK, "hello World!")
	})
	r.POST("/userLogin", handler.UserLogin)       //用户登录
	r.POST("/userRole", handler.UserRole)         //用户和角色
	r.POST("/roleNode", handler.RoleNode)         //角色和权限
	r.GET("/userList", handler.UserList)          //用户角色关联
	r.GET("/roleList", handler.RoleList)          //角色和权限关联
	r.POST("/shopAdd", handler.ShopAdd)           //商品添加
	r.POST("/shopOrderAdd", handler.ShopOrderAdd) //订单添加
	r.GET("/shopList", handler.ShopList)          //订单列表
	r.GET("/shopDetail", handler.ShopDetail)      //订单详情
	return r
}
